package com.chinawlx.wlxteacher.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.WLXRxBus;
import com.chinawlx.wlxteacher.network.bean.WLXHomeworkAddBean;
import com.chinawlx.wlxteacher.ui.event.SendImagesEvent;
import com.chinawlx.wlxteacher.utils.WLXActivityManagerUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.utils.WLXStringUtil;
import com.chinawlx.wlxteacher.widget.WLXCustomToast;
import com.chinawlx.wlxteacher.wlx_file;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import wlx.wm.photopicker.widget.MultiPickResultView;

/**
 * 作者:吴名
 * 时间:16/9/13
 * 描述:交作业(发送图片作业)
 */
public class WLXSendPictureActivity extends WLXBaseActivity {
    @BindView(R.id.tv_send)
    TextView mTvSend;
    @BindView(R.id.et_content)
    EditText mEtContent;
    @BindView(R.id.tv_textCount)
    TextView mTvTextCount;
    @BindView(R.id.multiPickResultView)
    MultiPickResultView mMultiPickResultView;

    private Context mContext;
    private String mFrom;//来自哪个activity
    private String mImagePath;//拍照及滤镜处理之后传递过来的一张图片的路径
    private String mContent;//作业文字内容
    /**
     * 保存将要发送的图片路径
     */
    private ArrayList<String> mImagePathList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        WLXActivityManagerUtil.addActivity(this);
        setContentView(R.layout.activity_send_picture);
        ButterKnife.bind(this);

        mFrom = getIntent().getStringExtra("from");

        if (mFrom.equals(WLXFilterActivity.class.getSimpleName())) {
            //自己拍照,然后滤镜处理后过来的
            mImagePath = getIntent().getStringExtra(WLXConstant.IMAGE_PATH);
            if (!WLXStringUtil.isEmpty(mImagePath)) {
                mImagePathList.add(mImagePath);
            }
            WLXLogUtil.printE("来源activity:" + mFrom + "图片路径:" + mImagePath);
        } else if (mFrom.equals(WLXRecordVideoPhotoActivity.class.getSimpleName())) {
            //不拍照,直接在拍照界面进入相册
            // TODO: 16/9/18 直接跳转到相册界面
        }
        initViews();
    }

    private void initViews() {
        //显示字数
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mTvTextCount.setText(String.format("%s/150", String.valueOf(editable.length())));
            }
        });

        //初始化已选择的图片
        mMultiPickResultView.init(this, MultiPickResultView.ACTION_SELECT, mImagePathList);
    }

    public void backMain(View view) {
        // TODO: 16/9/14 是否放弃本次编辑,弹框显示
        onBackPressed();
    }

    @OnClick(R.id.tv_send)
    public void onClick() {
        //=======================上传图片作业(至少有一张)
        if (mMultiPickResultView.getPhotos().size() > 0) {
            mContent = mEtContent.getText().toString();
            addHomeWork();
        } else {
            WLXCustomToast.show("至少上传一张图片");
        }

    }

    /**
     * 告诉服务器要交作业作业
     */
    private void addHomeWork() {
        WLXHttpRxHelper.getInstance().homeworkAddForImage(WLXConstant.student_id, WLXConstant
                .grade_id, mContent, "image", mMultiPickResultView.getPhotos().size() + "", new
                Subscriber<WLXHomeworkAddBean>() {


            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(WLXHomeworkAddBean homeworkAddBean) {
                if (homeworkAddBean.getCode() == 0) {
                    WLXConstant.RESOURCE_ID = homeworkAddBean.getData().getId() + "";
                    //存数据库关于视频信息
                    wlx_file file = new wlx_file(WLXConstant.RESOURCE_ID, "homework", 0, 0, "",
                            "waiting", "", System.currentTimeMillis() / 1000);
                    WLXGreenDaoUtil.getFileDao().insert(file);
                    //========================发送事件到主界面去发送图片
                    WLXConstant.IMAGES_PATH.clear();
                    WLXConstant.IMAGES_PATH.addAll(mMultiPickResultView.getPhotos());
                    WLXRxBus.getInstance().send(new SendImagesEvent());
                    finish();
                } else {
                    WLXCustomToast.show(homeworkAddBean.getMsg());
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mMultiPickResultView.onActivityResult(requestCode, resultCode, data);
    }

}
